import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {CommonService} from '../common.service';

@Component({
    selector: 'app-paginator',
    templateUrl: './paginator.component.html',
    styleUrls: ['./paginator.component.css']
})
export class PaginatorComponent implements OnInit {
    @Input() total: any;  // 总数据条数
    @Input() pageSize: number; // 每页数条数
    @Input() totalPage: number; // 总页数
    @Input() curPage: number; // 当前页码
    @Input() channel: string; // 频道
    @Input() params: any; // 参数
    @Output() toLink = new EventEmitter();
    url;

    constructor(private Service: CommonService) {
    }

    ngOnInit() {
        this.getPageUrl(2);
    }


    getPageUrl(page) {
        let params = {};
        let url = '';
        if (this.channel == 'comment') {
            url = 'comment/getlist';
        } else {
            params = this.Service.getUrlParams();
            url = this.Service.getUrlPath();
        }

        let urlParams = '';
        params['page'] = page;

        for (const i in params) {
            urlParams += i + '=' + params[i] + '&';
        }
        urlParams = urlParams.substring(0, urlParams.lastIndexOf('&'));
        return '/' + url + '?' + urlParams;
    }

    getPageList(total, pageSize, totalPage, curPage) {
        /**
         * 分页设置
         */
        const pageList = [];
        const sise = 2;
        const window = sise * 2;

        if (totalPage < window + 6) {   // 如果总数小于 pageSize，直接将代码放进去
            // 总数大于pageData，不用进来
            for (let i = 1; i <= totalPage; i++) {
                pageList.push({
                    pageNo: i
                });
            }

        } else if (curPage <= window) {
            // 如果总的页码数减去当前页码数小于页数差，那么直接计算出来显示
            for (let i = 1; i <= window + 1; i++) {
                pageList.push({
                    pageNo: i
                });
            }
            pageList.push({
                pageNo: '...'
            });
            pageList.push({
                pageNo: totalPage
            });
        } else if (curPage > (totalPage - window)) {
            pageList.push({
                pageNo: 1
            });
            pageList.push({
                pageNo: '...'
            });
            for (let i = totalPage - (window + 1); i <= totalPage; i++) {
                pageList.push({
                    pageNo: i
                });
            }
        } else {
            pageList.push({
                pageNo: 1
            });
            pageList.push({
                pageNo: '...'
            });
            for (let i = curPage - sise; i <= curPage + sise; i++) {
                pageList.push({
                    pageNo: i
                });
            }
            pageList.push({
                pageNo: '...'
            });
            pageList.push({
                pageNo: totalPage
            });
        }
        return pageList;
    }

    goLink(url, curPre = 0, page = 0) {
        if (this.channel == 'comment') {
            this.url = url;
            return this.toLink.emit(this);
        } else {
            if (curPre !== page) {
                location.href = url;
            }
        }
    }
}
